/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package df.servlet.interfacefile;
import df.bean.db.conn.DBConn;
import df.bean.db.conn.DBConnection;
import df.bean.interfacefile.ExportDFToAPBean;
import df.bean.interfacefile.ExportDFToBankBean;
import df.bean.interfacefile.ExportDFToGLBean;
import df.bean.interfacefile.ExportDFToPayrollBean;
import df.bean.obj.util.JDate;
import java.io.*;
import java.net.*;
import df.bean.obj.util.Variables;
import javax.servlet.*;
import javax.servlet.http.*;
import javazoom.upload.MultipartFormDataRequest;
import javazoom.upload.UploadBean;
import javazoom.upload.UploadException;

/**
 *
 * @author nopphadon
 */
public class ExportFileSrvl extends HttpServlet {
    DBConnection con1;
    DBConn cdb;
    HttpSession session;
    ExportDFToBankBean edtb = new ExportDFToBankBean();
    ExportDFToAPBean edfap = new ExportDFToAPBean();
    ExportDFToPayrollBean edfp = new ExportDFToPayrollBean();
    ExportDFToGLBean edgl = new ExportDFToGLBean();
    
    public void init() throws ServletException {
        super.init();

        con1 = new DBConnection();
        con1.connectToServer();
        cdb = new DBConn(con1.getConnection());
        try {
            cdb.setStatement();
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }
    
    @Override
    public void destroy(){
        super.destroy();
        con1.freeConnection();
    }
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        session = request.getSession(true);
        String hospital_code = session.getAttribute("HOSPITAL_CODE").toString();
        String save_file = request.getParameter("target_file");
        String process_name = request.getParameter("PROCESS_NAME");
        String pay_type = request.getParameter("PAY_TYPE");
        String month = request.getParameter("MM");
        String year = request.getParameter("YYYY");
        String bank_type = request.getParameter("BANK_TYPE");
        String transaction_date = request.getParameter("TRANSACTION_DATE");
        String payment_date = request.getParameter("PAYMENT_DATE");
        //path variable for upload or writefile Use for Windows OS
        //String path = getServletConfig().getServletContext().getRealPath("")+"\\reports\\output\\"+save_file+".txt";
        
        //path variable for upload or writefile Use for Linux OS
        String path = getServletConfig().getServletContext().getRealPath("")+"/reports/output/"+save_file+".txt";

        //link variable for show webpage link
        String link = "./reports/output/";

        try {
            if(process_name.equals("ExportAP") ){
            	edfap.setPaymentDate(payment_date);
            	edfap.setTransactionDate(transaction_date);
                reportGenerateFile(link + save_file+".txt", response, edfap.exportData(save_file, hospital_code, pay_type, year, month, cdb, path));
            }
            if(process_name.equals("ExportBank") ){
            	edtb.setOwnerBank(bank_type);
                reportGenerateFile(link + save_file+".txt", response, edtb.exportData(save_file, hospital_code, pay_type, year, month, cdb, path));
            }
            if(process_name.equals("ExportPayroll") ){
                reportGenerateFile(link + save_file+".txt", response, edfp.exportData(save_file, hospital_code, pay_type, year, month, cdb, path));
            }
            if(process_name.equals("ExportGL")){
            	edgl.setTransactionDate(transaction_date);
                reportGenerateFile(link + save_file+".txt", response, edgl.exportDataVTN(save_file, hospital_code, "GL", year, month, cdb, path));
            }
            if(process_name.equals("ExportAC")){
            	edgl.setTransactionDate(transaction_date);
                reportGenerateFile(link + save_file+".txt", response, edgl.exportDataVTN(save_file, hospital_code, "AC", year, month, cdb, path));
            }
        } finally { 
            //out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
    
    private void reportGenerateFile(String filename, HttpServletResponse response, boolean status){
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException ex) {System.out.println("PrintWriter Ex : "+ex);}
        System.out.println(filename);
        if(status){
            response.setContentType("text/html");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Generate Report Servlet</title>");  
            out.println("</head>");
            out.println("<body bgcolor=\"#dde4e8\">");
            out.println("<h1 align=\"center\">Report Generate File</h1>");
            out.println("<p align=\"center\">" +
                        "<a href=\""+filename+"\">" +
                        "<img src=\"./images/txt_icon.gif\" width=\"50\" height=\"50\" border=\"0\"/></a></p><br>");
            out.println("<p align=\"center\">Write File Report Complete</p><br>");
            out.println("</body>");
            out.println("</html>");
        }else{
            response.setContentType("text/html");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>View Report Servlet</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Report Result</h1>");
            out.println("<p align=\"center\">Can't write file interface</p>");
            out.println("</body>");
            out.println("</html>");
        }
    }
}
